// QQ表情插件
(function($){  
	$.fn.qqFace = function(options){
		var defaults = {
			id : 'facebox',
			path : 'face/',
			assign : 'content',
			tip : 'em_'
		};
		var option = $.extend(defaults, options);
		//var assign = $('#'+option.assign);
		
		var id = option.id;
		var path = option.path;
		var tip = option.tip;
		
/*		if(assign.length<=0){
			alert('缺少表情赋值对象。');
			return false;
		}*/
	$(this).each(function(i){ 
		var assign_id=$(this).attr("data-id");
		var assign=$('#'+option.assign+assign_id);
		$(this).click(function(e){
			var strFace, labFace;
			if($('#'+id).length<=0){
				strFace = '<div id=\"facebox\" style=\"position: absolute; z-index: 1000; top: 1254px; left: 125px;\" class=\"qqFace\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tbody>'+
					'<tr><td><img src=\"'+path+'0.gif\" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[微笑]\');"></td>'+
					'<td><img src="'+path+'1.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[撇嘴]\');"></td>'+
					'<td><img src="'+path+'2.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[色]\');"></td>'+
					'<td><img src="'+path+'3.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[发呆]\');"></td>'+
					'<td><img src="'+path+'4.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[流泪]\');"></td>'+
					'<td><img src="'+path+'5.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[害羞]\');"></td>'+
					'<td><img src="'+path+'6.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[闭嘴]\');"></td>'+
					'<td><img src="'+path+'7.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[睡]\');"></td>'+
					'<td><img src="'+path+'8.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[大哭]\');"></td>'+
					'<td><img src="'+path+'9.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[尴尬]\');"></td>'+
					'<td><img src="'+path+'10.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[发怒]\');"></td>'+
					'<td><img src="'+path+'11.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[调皮]\');"></td>'+
					'<td><img src="'+path+'12.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[呲牙]\');"></td>'+
					'<td><img src="'+path+'13.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[惊讶]\');"></td>'+
					'<td><img src="'+path+'14.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[难过]\');"></td>'+
					'</tr><tr>'+
					'<td><img src="'+path+'15.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[冷汗]\');"></td>'+
					'<td><img src="'+path+'16.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[抓狂]\');"></td>'+
					'<td><img src="'+path+'17.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[吐]\');"></td>'+
					'<td><img src="'+path+'18.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[偷笑]\');"></td>'+
					'<td><img src="'+path+'19.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[可爱]\');"></td>'+
					'<td><img src="'+path+'20.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[白眼]\');"></td>'+
					'<td><img src="'+path+'21.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[傲慢]\');"></td>'+
					'<td><img src="'+path+'22.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[饥饿]\');"></td>'+
					'<td><img src="'+path+'23.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[困]\');"></td>'+
					'<td><img src="'+path+'24.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[惊恐]\');"></td>'+
					'<td><img src="'+path+'25.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[流汗]\');"></td>'+
					'<td><img src="'+path+'26.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[憨笑]\');"></td>'+
					'<td><img src="'+path+'27.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[大兵]\');"></td>'+
					'<td><img src="'+path+'28.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[奋斗]\');"></td>'+
					'<td><img src="'+path+'29.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[咒骂]\');"></td>'+
					'</tr><tr>'+
					'<td><img src="'+path+'30.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[疑问]\');"></td>'+
					'<td><img src="'+path+'31.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[嘘]\');"></td>'+
					'<td><img src="'+path+'32.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[晕]\');"></td>'+
					'<td><img src="'+path+'33.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[折磨]\');"></td>'+
					'<td><img src="'+path+'34.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[衰]\');"></td>'+
					'<td><img src="'+path+'35.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[敲打]\');"></td>'+
					'<td><img src="'+path+'36.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[再见]\');"></td>'+
					'<td><img src="'+path+'37.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[擦汗]\');"></td>'+
					'<td><img src="'+path+'38.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[抠鼻]\');"></td>'+
					'<td><img src="'+path+'39.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[糗大了]\');"></td>'+
					'<td><img src="'+path+'40.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[坏笑]\');"></td>'+
					'<td><img src="'+path+'41.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[左哼哼]\');"></td>'+
					'<td><img src="'+path+'42.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[右哼哼]\');"></td>'+
					'<td><img src="'+path+'43.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[哈欠]\');"></td>'+
					'<td><img src="'+path+'44.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[鄙视]\');"></td>'+
					'</tr><tr>'+
					'<td><img src="'+path+'45.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[委屈]\');"></td>'+
					'<td><img src="'+path+'46.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[快哭了]\');"></td>'+
					'<td><img src="'+path+'47.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[阴险]\');"></td>'+
					'<td><img src="'+path+'48.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[亲亲]\');"></td>'+
					'<td><img src="'+path+'49.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[吓]\');"></td>'+
					'<td><img src="'+path+'50.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[可怜]\');"></td>'+
					'<td><img src="'+path+'51.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[拥抱]\');"></td>'+
					'<td><img src="'+path+'52.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[月亮]\');"></td>'+
					'<td><img src="'+path+'53.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[太阳]\');"></td>'+
					'<td><img src="'+path+'54.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[炸弹]\');"></td>'+
					'<td><img src="'+path+'55.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[骷髅]\');"></td>'+
					'<td><img src="'+path+'56.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[菜刀]\');"></td>'+
					'<td><img src="'+path+'57.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[猪头]\');"></td>'+
					'<td><img src="'+path+'58.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[西瓜]\');"></td>'+
					'<td><img src="'+path+'59.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[咖啡]\');"></td>'+
					'</tr><tr>'+
					'<td><img src="'+path+'60.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[饭]\');"></td>'+
					'<td><img src="'+path+'61.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[爱心]\');"></td>'+
					'<td><img src="'+path+'62.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[强]\');"></td>'+
					'<td><img src="'+path+'63.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[弱]\');"></td>'+
					'<td><img src="'+path+'64.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[握手]\');"></td>'+
					'<td><img src="'+path+'65.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[胜利]\');"></td>'+
					'<td><img src="'+path+'66.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[抱拳]\');"></td>'+
					'<td><img src="'+path+'67.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[勾引]\');"></td>'+
					'<td><img src="'+path+'68.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[OK]\');"></td>'+
					'<td><img src="'+path+'69.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[NO]\');"></td>'+
					'<td><img src="'+path+'70.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[玫瑰]\');"></td>'+
					'<td><img src="'+path+'71.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[凋谢]\');"></td>'+
					'<td><img src="'+path+'72.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[示爱]\');"></td>'+
					'<td><img src="'+path+'73.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[爱情]\');"></td>'+
					'<td><img src="'+path+'74.gif" onclick=\"$(\'#'+option.assign+assign_id+'\').setCaret();$(\'#'+option.assign+assign_id+'\').insertAtCaret(\'[飞吻]\');"></td>'+
					'</tr>'+
					'<tr></tr>'+
					'</tbody></table></div>';
			}
			$(this).parent().append(strFace);
			var offset = $(this).position();
			var top = offset.top + $(this).outerHeight();
			$('#'+id).css('top',top);
			$('#'+id).css('left',offset.left);
			$('#'+id).show();
			e.stopPropagation();
		});
});
		$(document).click(function(){
			$('#'+id).hide();
			$('#'+id).remove();
		});
	};
})(jQuery);

jQuery.extend({ 
unselectContents: function(){ 
	if(window.getSelection) 
		window.getSelection().removeAllRanges(); 
	else if(document.selection) 
		document.selection.empty(); 
	} 
}); 
jQuery.fn.extend({ 
	selectContents: function(){ 
		$(this).each(function(i){ 
			var node = this; 
			var selection, range, doc, win; 
			if ((doc = node.ownerDocument) && (win = doc.defaultView) && typeof win.getSelection != 'undefined' && typeof doc.createRange != 'undefined' && (selection = window.getSelection()) && typeof selection.removeAllRanges != 'undefined'){ 
				range = doc.createRange(); 
				range.selectNode(node); 
				if(i == 0){ 
					selection.removeAllRanges(); 
				} 
				selection.addRange(range); 
			} else if (document.body && typeof document.body.createTextRange != 'undefined' && (range = document.body.createTextRange())){ 
				range.moveToElementText(node); 
				range.select(); 
			} 
		}); 
	}, 

	setCaret: function(){ 
		if(!$.support) return; 
		var initSetCaret = function(){ 
			var textObj = $(this).get(0); 
			
		}; 
		$(this).click(initSetCaret).select(initSetCaret).keyup(initSetCaret); 
	}, 

	insertAtCaret: function(textFeildValue){ 
		var textObj = $(this).get(0);
		var selection;
		if(document.all && textObj.createTextRange && textObj.caretPos){ 
			var caretPos=textObj.caretPos; 
			caretPos.text = caretPos.text.charAt(caretPos.text.length-1) == '' ? 
			textFeildValue+'' : textFeildValue; 
		} else if(textObj.setSelectionRange){ 
			var rangeStart=textObj.selectionStart; 
			var rangeEnd=textObj.selectionEnd; 
			var tempStr1=textObj.value.substring(0,rangeStart); 
			var tempStr2=textObj.value.substring(rangeEnd); 
			textObj.value=tempStr1+textFeildValue+tempStr2; 
			textObj.focus(); 
			var len=textFeildValue.length; 
			textObj.setSelectionRange(rangeStart+len,rangeStart+len); 
			textObj.blur(); 
		}else{ 
			textObj.value+=textFeildValue; 
		} 
	} 
});